import errorMsg from 'chain33errori18n'
export default {
  data() {
    return {
      param: '',
      result: '',
      isDisabled: false
    }
  },
  methods: {
    dealMsg(data) {
      if (data.error == null) {
        this.result = data.result;
        this.$notify({
          title: this.$t('components.tip'),
          message: this.$t('components.succ'),
          type: "success",
          duration: 1500
        });
      } else {
        this.result = ''
        this.$notify({
          title: this.$t('components.tip'),
          message: errorMsg(data.error),
          type: "warning",
          duration: 1500
        });
      }
    },
    submit(type) {
      if (this.isDisabled) return;
      this.isDisabled = true;
      // 去掉首尾空格
      this.param = this.param.trim()

      if (!this.param) {
        this.$notify({
          title: this.$t('components.tip'),
          message: this.$t('components.errTxt'),
          type: 'warning'
        })
        this.isDisabled = false
        return false;
      }
      if (this.param.slice(0, 2) === '0x') this.param = this.param.slice(2, this.param.length)
      if (!/^[0-9a-fA-F]+$/.test(this.param)) {
        this.$notify({
          title: this.$t('components.tip'),
          message: this.$t('components.errFormat'),
          type: 'warning'
        })
        this.isDisabled = false
        return false;
      }

      if (type === 'Ptx') {
        this.Rpc.sendTransaction(this.param).then(data => {
          this.dealMsg(data)
          this.isDisabled = false;
        });
      } else {
        this.Rpc.decodeRawTransaction(this.param).then((data) => {
          this.dealMsg(data)
          this.isDisabled = false;
        });
      }
    }
  }
}